package com.paopao.loansheet.service;

import com.paopao.common.ServiceHandler;
import com.paopao.domain.ItemInfoBean;
import com.paopao.domain.ObjectListResult;
import com.paopao.domain.R;
import com.paopao.mapper.ItemInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemInfoService {
    @Autowired
    private ItemInfoMapper itemInfoMapper;

    @Autowired
    private ServiceHandler serviceHandler;

    public R addItem(ItemInfoBean itemInfoBean) {

        Integer res = itemInfoMapper.addItemInfo(itemInfoBean);
        if (res > 0) {
            return new R(200, "OK");
        }else {
            return new R(-1, "error");
        }
    }

    public R deleteItem(Integer id) {
        Integer res = itemInfoMapper.deleteItemInfo(id);
        if (res > 0) {
            return new R(200, "OK");
        }else {
            return new R(-1, "error");
        }
    }

    public R editItem(ItemInfoBean itemInfoBean) {
        Integer res = itemInfoMapper.editItemInfo(itemInfoBean);
        if (res > 0) {
            return new R(200, "OK");
        }else {
            return new R(-1, "error");
        }
    }

    public R selectItem(int id) {
        ItemInfoBean itemInfoBean = itemInfoMapper.selectItemInfoByid(id);
        if (itemInfoBean != null){
            return new R(200, "OK", itemInfoBean);
        }else {
            return new R(-1, "error");
        }
    }

    public R selectItemList(Integer pageNum, Integer pageSize, ItemInfoBean itemInfoBean) {
        List<ItemInfoBean> list = itemInfoMapper.getItemList(itemInfoBean);
        ObjectListResult listResult = serviceHandler.paging(list);

        if (listResult != null && listResult.getList().size() > 0){
            return new R(200, "OK", listResult);
        }else{
            return new R(200, "OK");
        }
    }
}
